package com.kk.xhr.admin.controller;

import com.kk.xhr.admin.biz.IRoleBizService;
import com.kk.xhr.admin.controller.request.RoleAddRequest;
import com.kk.xhr.admin.controller.request.RoleModifyRequest;
import com.kk.xhr.admin.controller.vo.RoleVO;
import com.kk.xhr.common.pojo.BinaryEnum;
import com.kk.xhr.web.vo.ApiResult;
import com.kk.xhr.web.vo.PageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.Executor;

/**
 * 角色控制器
 *
 * @author kkmystery
 * @version 1.0 2021/3/11
 * @since 1.0.0
 */
@RestController
@RequiredArgsConstructor
@Api(value = "角色管理", tags = "角色管理")
@RequestMapping("/role")
public class RoleController {
    private final Executor adminControllerBizExecutor;
    private final IRoleBizService roleBizService;

    @ApiOperation(value = "获取角色信息")
    @RequiresRoles(value = {"admin"})
    @GetMapping
    public CompletionStage<ApiResult<PageVO<RoleVO>>> query() {
        return CompletableFuture.supplyAsync(() ->
            ApiResult.success(roleBizService.query()), adminControllerBizExecutor);
    }

    @ApiOperation(value = "添加角色信息")
    @RequiresRoles(value = {"admin"})
    @PostMapping
    public CompletionStage<ApiResult<Long>> add(@RequestBody RoleAddRequest request) {
        return CompletableFuture.supplyAsync(() -> {
            Long roleId = roleBizService.add(request);
            return ApiResult.success(roleId);
        }, adminControllerBizExecutor);
    }

    @ApiOperation(value = "给用户添加角色")
    @RequiresRoles(value = {"admin"})
    @PostMapping("/user/{userId}")
    public CompletionStage<ApiResult<Void>> addUserRole(@PathVariable("userId") Long userId, @RequestBody List<Long> ids) {
        return CompletableFuture.supplyAsync(() -> {
            roleBizService.addUserRole(userId, ids);
            return ApiResult.success();
        }, adminControllerBizExecutor);
    }

    @ApiOperation(value = "修改角色信息")
    @RequiresRoles(value = {"admin"})
    @PutMapping("/{id}")
    public CompletionStage<ApiResult<Void>> modify(@PathVariable("id") Long id, @RequestBody RoleModifyRequest request) {
        return CompletableFuture.supplyAsync(() -> {
            roleBizService.modify(id, request);
            return ApiResult.success();
        }, adminControllerBizExecutor);
    }

    @ApiOperation(value = "修改角色状态")
    @RequiresRoles(value = {"admin"})
    @PutMapping("/status/{id}")
    public CompletionStage<ApiResult<Void>> modifyStatus(@PathVariable("id") Long id, BinaryEnum status) {
        return CompletableFuture.supplyAsync(() -> {
            roleBizService.modify(id, status);
            return ApiResult.success();
        }, adminControllerBizExecutor);
    }
}


